<?php
defined('IN_PHPCMS') or exit('No permission resources.');

class sitemap{
	function __construct() {
		pc_base::load_app_func('global','admin');
		$this->header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
	    $this->charset = "UTF-8";
	    $this->footer = "\t</urlset>\n";
		$this->items = array();
		//生成栏目级别选项
		$this->siteid = get_siteid();
		$this->categorys = getcache('category_content_'.$this->siteid,'commons');
	}
	
	function add_item2($new_item) {
        $this->items[] = $new_item;
    }
    function google_sitemap_item($loc, $lastmod = '', $changefreq = '', $priority = '') {
		$data = array();
		$data['loc'] =  $loc;
		$data['lastmod'] =  $lastmod;
		$data['changefreq'] =  $changefreq;
		$data['priority'] =  $priority;
		return $data;
    }
    function build( $file_name = null ) {
        $map = $this->header . "\n";
        foreach ($this->items AS $item){
            $map .= "\t\t<url>\n\t\t\t<loc>$item[loc]</loc>\n";
            $map .= "\t\t\t<lastmod>$item[lastmod]</lastmod>\n";
            $map .= "\t\t\t<changefreq>$item[changefreq]</changefreq>\n";
            $map .= "\t\t\t<priority>$item[priority]</priority>\n";
            $map .= "\t\t</url>\n\n";
        }
        $map .= $this->footer . "\n";
        if (!is_null($file_name)){
            	return file_put_contents($file_name, $map);
        	} else {
            	return $map;
        }
    }
    /*
     * 地图自动生成
	 */
	function autoset(){
		$hits_db = pc_base::load_model('hits_model');
		$siteid = $this->siteid;//读站点缓存
		$sitecache = getcache('sitelist','commons');
		//根据当前站点,取得文件存放路径
  		$systemconfig = pc_base::load_config('system');
 		$html_root = substr($systemconfig['html_root'], 1);
 		//判断当前站点目录,是PHPCMS则把文件写到根目录下, 不是则写到分站目录下.(分站目录用由静态文件路经html_root和分站目录dirname组成)
 		if($siteid==1){
 			$dir = PHPCMS_PATH;
 		}else {
 			$dir = PHPCMS_PATH.$html_root.DIRECTORY_SEPARATOR.$sitecache[$siteid]['dirname'].DIRECTORY_SEPARATOR;
 		}
 		//模型缓存
 		$modelcache = getcache('model','commons');
 		
 		//获取当前站点域名,下面生成URL时会用到.
 		$this_domain = substr($sitecache[$siteid]['domain'], 0,strlen($sitecache[$siteid]['domain'])-1);
 		//生成网站地图
		$content_priority = '0.8';
		$content_changefreq = 'weekly'; 
		$num = '5000';
		
		$today = date('Y-m-d');
		$domain = $this_domain;
		if($domain == "/" || empty($domain)){
			$domain = SITE_PROTOCOL.SITE_URL;
		}
		//生成地图头部　－第一条
		$smi = $this->google_sitemap_item($domain, $today, 'daily', '1.0');
		$this->add_item2($smi);
			
	    $this->content_db = pc_base::load_model('content_model');
	    //只提取该站点的模型.再循环取数据,生成站点地图.
		$modelcache = getcache('model','commons');
		$new_model = array();
		foreach ($modelcache as $modelid => $mod){
			if($mod['siteid']==$siteid){
				$new_model[$modelid]['modelid'] = $modelid;
				$new_model[$modelid]['name'] = $mod['name'];						
			}
			}
		foreach($new_model as $modelid=>$m) {//每个模块取出num条数据 
			$this->content_db->set_model($modelid);// 或者 $this->conetnt_db->set_model($modelid);
			$result = $this->content_db->select(array('status'=>99), '*', $limit = "0,$num", $order = 'inputtime desc');
			foreach ($result as $arr){
				if(substr($arr['url'],0,1)=='/'){
					$url = new_html_special_chars(strip_tags($domain.$arr['url']));
				}else {
					$url = new_html_special_chars(strip_tags($arr['url']));
				}
				$hit_r = $hits_db->get_one(array('hitsid'=>'c-'.$modelid.'-'.$arr['id']));
				if($hit_r['views']>1000) $content_priority = 0.9;
				$smi    = $this->google_sitemap_item($url, $today, $content_changefreq, $content_priority);//推荐文件
				$this->add_item2($smi);
			}
		}
		
	    $sm_file = $dir.'sitemaps.xml';
		$this->build($sm_file);
	}
}